package com.guchenbo.spring.boot.kafka.api;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

/**
 * 老api，kafka生产者，topic为cre_kafka_api
 *
 * @Author gucb
 * @Date 2017/5/17.
 */
public class KafkaApiProducer {

    public static final String TOPIC = "octopus_sdk_task1";
//    public static final String TOPIC = "cre_kafka_api";

    private static Producer<String, String> producer;
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("bootstrap.servers", "iZuf6c3gcnqmp26nhnx9qoZ:9092");
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);
        sendMsg(producer);
    }

    private static void sendMsg(KafkaProducer producer) {
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            String ip = "192.168.1." + random.nextInt(255);
            String msg = simpleDateFormat.format(new Date()) + "," + ip;

            ProducerRecord<String, String> record = new ProducerRecord<>("test","val");
            producer.send(record);

        }

    }
}
